'''
信泰i立方恶性肿瘤疾病保险
'''
import sys

sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import execjs
from common.helper import Helper


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "信泰i立方恶性肿瘤疾病保险"

    # 生成产品数据
    def genProductInfo(self):

        plan = {

            "planA": {
                # 年龄段
                "year": [str(i) for i in range(0, 41)],
                # 性别
                "gender": ["M", "F"],
                # 保障期限
                "expire": ["终身", "70岁"],
                # 保额
                "money": [str(i) + "0000" for i in range(5, 35, 5)],
                # 缴费间隔
                "interval": ["5", "30"]
            },
            "planB": {
                # 年龄段
                "year": [str(i) for i in range(41, 56)],
                # 性别
                "gender": ["M", "F"],
                # 保障期限
                "expire": ["终身", "70岁"],
                # 保额
                "money": [str(i) + "0000" for i in range(5, 35, 5)],
                # 缴费间隔
                "interval": ["5", "30"]
            }

        }

        for k in plan:

            for v in product(plan[k]["year"], plan[k]["gender"], plan[k]["expire"], plan[k]["money"],
                             plan[k]["interval"]):
                # 写入数据库
                self.mysql.cursor.execute(domain[self.name]["insert_sql"].format(

                    v[0], v[1], v[2], v[3], v[4]
                ))
                self.mysql._conn.commit()
                print(domain[self.name]["insert_sql"].format(

                    v[0], v[1], v[2], v[3], v[4]
                )
                )

    # 计算保险费用
    def PremiumCalc(self, dt=None):

        data = {
            "num": dt["money"][::5],
            "age": dt["year"],
            "expire": "0" if dt["expire"] == "终身" else "1",
            "interval": "0" if dt["interval"] == "5" else "1",
            "gender": "0" if dt["gender"] == "M" else "1"
        }

        js = '''
        fl_a = [[[[
                "20.6",
                "21.1",
                "21.7",
                "22.4",
                "23.1",
                "23.8",
                "24.5",
                "25.3",
                "26.1",
                "27.0",
                "27.8",
                "28.7",
                "29.7",
                "30.6",
                "31.6",
                "32.6",
                "33.7",
                "34.8",
                "35.9",
                "37.1",
                "38.3",
                "39.5",
                "40.8",
                "42.2",
                "43.5",
                "45.0",
                "46.4",
                "47.9",
                "49.5",
                "51.1",
                "52.7",
                "54.4",
                "56.1",
                "57.9",
                "59.7",
                "61.6",
                "63.5",
                "65.4",
                "67.4",
                "69.4",
                "71.5",
                "73.7",
                "75.8",
                "78.0",
                "80.3",
                "82.5",
                "84.8",
                "87.0",
                "89.3",
                "91.6",
                "93.9",
                "96.1",
                "98.3",
                "100.5",
                "102.7",
                "104.8"
              ],[
                "4.9",
                "5.0",
                "5.2",
                "5.3",
                "5.5",
                "5.7",
                "5.9",
                "6.0",
                "6.2",
                "6.4",
                "6.6",
                "6.9",
                "7.1",
                "7.3",
                "7.6",
                "7.8",
                "8.1",
                "8.3",
                "8.6",
                "8.9",
                "9.2",
                "9.5",
                "9.8",
                "10.1",
                "10.5",
                "10.8",
                "11.2",
                "11.6",
                "12.0",
                "12.4",
                "12.8",
                "13.2",
                "13.7",
                "14.1",
                "14.6",
                "15.1",
                "15.6",
                "16.2",
                "16.7",
                "17.3",
                "17.9"]],[[
                "12.3",
                "12.5",
                "12.8",
                "13.2",
                "13.5",
                "13.9",
                "14.3",
                "14.7",
                "15.2",
                "15.6",
                "16.1",
                "16.6",
                "17.1",
                "17.6",
                "18.1",
                "18.7",
                "19.2",
                "19.8",
                "20.4",
                "21.0",
                "21.7",
                "22.3",
                "23.0",
                "23.7",
                "24.4",
                "25.1",
                "25.9",
                "26.6",
                "27.4",
                "28.2",
                "29.1",
                "29.9",
                "30.7",
                "31.6",
                "32.5",
                "33.3",
                "34.2",
                "35.1",
                "36.0",
                "36.9",
                "37.7",
                "38.6",
                "39.5",
                "40.3",
                "41.1",
                "41.9",
                "42.5",
                "43.2",
                "43.7",
                "44.2",
                "44.5",
                "44.7",
                "44.8",
                "44.8",
                "44.6",
                "44.2"
              ],
              [
                "2.9",
                "3.0",
                "3.1",
                "3.1",
                "3.2",
                "3.3",
                "3.4",
                "3.5",
                "3.6",
                "3.7",
                "3.8",
                "4.0",
                "4.1",
                "4.2",
                "4.3",
                "4.5",
                "4.6",
                "4.7",
                "4.9",
                "5.0",
                "5.2",
                "5.3",
                "5.5",
                "5.7",
                "5.9",
                "6.0",
                "6.2",
                "6.4",
                "6.6",
                "6.8",
                "7.0",
                "7.3",
                "7.5",
                "7.7",
                "7.9",
                "8.2",
                "8.4",
                "8.7",
                "8.9",
                "9.2",
                "9.4"
              ]
            ]
          ],
          [
            [
              [
                "20.5",
                "21.0",
                "21.6",
                "22.2",
                "22.9",
                "23.6",
                "24.4",
                "25.1",
                "25.9",
                "26.8",
                "27.6",
                "28.5",
                "29.4",
                "30.4",
                "31.3",
                "32.3",
                "33.3",
                "34.4",
                "35.5",
                "36.6",
                "37.7",
                "38.9",
                "40.1",
                "41.4",
                "42.6",
                "43.9",
                "45.3",
                "46.6",
                "48.0",
                "49.4",
                "50.9",
                "52.3",
                "53.8",
                "55.2",
                "56.7",
                "58.2",
                "59.7",
                "61.2",
                "62.7",
                "64.1",
                "65.6",
                "67.0",
                "68.4",
                "69.8",
                "71.1",
                "72.4",
                "73.6",
                "74.8",
                "76.0",
                "77.1",
                "78.3",
                "79.4",
                "80.6",
                "81.7",
                "82.8",
                "83.9"
              ],
              [
                "4.9",
                "5.0",
                "5.2",
                "5.3",
                "5.5",
                "5.6",
                "5.8",
                "6.0",
                "6.2",
                "6.4",
                "6.6",
                "6.8",
                "7.0",
                "7.3",
                "7.5",
                "7.7",
                "8.0",
                "8.2",
                "8.5",
                "8.8",
                "9.1",
                "9.4",
                "9.7",
                "10.0",
                "10.3",
                "10.6",
                "10.9",
                "11.3",
                "11.6",
                "12.0",
                "12.4",
                "12.8",
                "13.1",
                "13.5",
                "13.9",
                "14.3",
                "14.7",
                "15.1",
                "15.5",
                "15.9",
                "16.3"
              ]
            ],
            [
              [
                "13.9",
                "14.2",
                "14.5",
                "14.9",
                "15.3",
                "15.7",
                "16.2",
                "16.7",
                "17.2",
                "17.7",
                "18.2",
                "18.8",
                "19.4",
                "19.9",
                "20.5",
                "21.1",
                "21.8",
                "22.4",
                "23.1",
                "23.7",
                "24.4",
                "25.1",
                "25.8",
                "26.6",
                "27.3",
                "28.1",
                "28.9",
                "29.6",
                "30.4",
                "31.2",
                "32.0",
                "32.7",
                "33.5",
                "34.2",
                "34.9",
                "35.6",
                "36.3",
                "36.9",
                "37.5",
                "38.0",
                "38.5",
                "38.9",
                "39.2",
                "39.4",
                "39.6",
                "39.6",
                "39.6",
                "39.5",
                "39.3",
                "39.0",
                "38.6",
                "38.1",
                "37.6",
                "37.0",
                "36.3",
                "35.5"
              ],
              [
                "3.3",
                "3.4",
                "3.5",
                "3.5",
                "3.6",
                "3.7",
                "3.9",
                "4.0",
                "4.1",
                "4.2",
                "4.4",
                "4.5",
                "4.6",
                "4.8",
                "4.9",
                "5.1",
                "5.2",
                "5.4",
                "5.5",
                "5.7",
                "5.9",
                "6.0",
                "6.2",
                "6.4",
                "6.6",
                "6.8",
                "7.0",
                "7.2",
                "7.4",
                "7.6",
                "7.8",
                "8.0",
                "8.2",
                "8.4",
                "8.6",
                "8.7",
                "8.9",
                "9.1",
                "9.3",
                "9.4",
                "9.6"
              ]
            ]
          ]
        ]
        function calculate( data ){
		//0是男，1是女
			var num = data["num"];		
			var age = data["age"];
			var other = data["expire"];//保险期间
			var period = data["interval"];
			var sex=data["gender"];
			console.log("["+parseInt(sex)+"]["+parseInt(other) + "][" + parseInt(period) +"][" + parseInt(age) + "]");
			var mainriskfee = fl_a[parseInt(sex)][parseInt(other)][parseInt(period)][age];
			
			mainriskfee = (num * 10 * mainriskfee).toFixed(2);
			return mainriskfee;
		
	    }
	    var data = {'num': '5', 'year': '55', 'expire': '1', 'interval': '0', 'gender': '0'}
	    result = calculate( data )
	    
	    console.log( result )
        '''

        cxt = execjs.compile( js )

        price = ( cxt.call("calculate",data) )

        if price == "NAN":

            data = {
                "name":self.name,
                "id":dt["id"],
                "state":3
            }
            self.mysql.updateTask( data )

        else:

            data = {
                "name":self.name,
                "id":dt["id"],
                "price":Helper.decimalParse(price)
            }
            self.mysql.updatePrice( data )

    def main(self):

        while True:

            # 随机领取一条数据
            dt = self.mysql.randTask(self.name)

            if not dt:

                print("产品价格抓取完成~~~")

                return

                # 任务执行状态
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 1
            }
            self.mysql.updateTask(data)

            self.PremiumCalc( dt )
